/*
 *  Copyright (C) 2005-2007 Renier Roth
 *  www.renier.de
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package de.renier.jkeepass.actions;

import java.awt.event.ActionEvent;
import java.io.File;
import java.io.FileOutputStream;

import javax.swing.AbstractAction;
import javax.swing.ImageIcon;
import javax.swing.JOptionPane;

import de.renier.jkeepass.JKeePass;
import de.renier.jkeepass.Messages;
import de.renier.jkeepass.dialog.CreatePasswordDialog;
import de.renier.jkeepass.encryption.Cryptor;
import de.renier.jkeepass.util.Utils;

/**
 * SaveAction
 *
 * @author <a href="mailto:software@renier.de">Renier Roth</a>
 */
public class SaveAction extends AbstractAction {

  private static final long serialVersionUID = -1539781875535778588L;
  public SaveAction() {
    super(Messages.getString("SaveAction.0"),new ImageIcon(OpenAction.class.getResource("/org/javalobby/icons/20x20/Save.gif"))); //$NON-NLS-1$ //$NON-NLS-2$
    this.setEnabled(false);
  }
  /* (non-Javadoc)
   * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
   */
  public void actionPerformed(ActionEvent e) {
    if (JKeePass.application.getOpenFile() != null && JKeePass.application.isModified()) {
      File saveFile = JKeePass.application.getOpenFile();
      try {
        // check if we have a password initialized
        if (!Cryptor.isInitialized()) {
          // call password dialog
          CreatePasswordDialog passDialog = new CreatePasswordDialog(JKeePass.application);
          passDialog.show();
          // get result
          if (passDialog.getResult() == CreatePasswordDialog.RESULT_OK) {
            // init cryptor
            Cryptor.initialize(passDialog.getPassword());
          } else {
            // cancel all
            return;
          }
        }
        // open output file
        FileOutputStream outFile = new FileOutputStream(saveFile);
        // output
        Utils.outputNode(outFile, JKeePass.application.getElementTreePanel().getRootNode());
        JKeePass.application.setModified(false);
      } catch (Exception ioe) {
        JOptionPane.showConfirmDialog(JKeePass.application,Messages.getString("SaveAction.2") + saveFile.getPath() + "\nError message: " + ioe.getMessage(), "Error" , JOptionPane.CLOSED_OPTION, JOptionPane.ERROR_MESSAGE); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
        ioe.printStackTrace();
      }
    } else {
      // call save as if no file
      if (JKeePass.application.getOpenFile() == null) {
        ActionManager.getSaveAsAction().actionPerformed(e);
      }
    }

  }

}
